import { getTemplatRemote } from '@/api/flow/template';
import _ from 'lodash';
import remote from './props';

export default {
  mixins: [remote],

  methods: {
    // 远程数据
    getRemote() {
      return new Promise((resolve, reject) => {
        const { use, fid } = this.search;
        const vid = (use && fid && this.getVidValue()) || undefined;
        getTemplatRemote(this.remoteUrl, { inputValue: this.id, vid })
          .then((data) => {
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },

    // get vale
    getElementValue(getElements) {
      if (getElements) {
        const { fid } = this.search;
        const elements = getElements();
        const index = _
          .findIndex(elements, ({ id }) => (id === fid));
        return elements[index];
      }
      // 1\清空
      // 2\获取获取关联值
    },

    // 获取组件
    getRefs() {
      // table
      if (this.vanFormTable) {
        const { getElements } = this.vanFormTable;
        const ele = this.getElementValue(getElements);
        // this.ref_child.indexOf(id || '')
        if (ele) {
          return this.vanFormTable;
        }
        return this.vanFormGenerator;
      }
      return this.vanFormGenerator;
    },

    // 获取关联值
    getVidValue() {
      // if (this.search) false
      const { getElements } = this.getRefs() || {};
      const { infoValue = '' } = this.getElementValue(getElements) || '';

      return infoValue;
    },

    // 清空关联组件
    clearRefsChild(getElements) {
      const elements = getElements || (() => ([]));
      let isTab = false;
      elements().forEach(({ id, resetValue }) => {
        const index = this.ref_child.indexOf(id);
        if (index > -1) {
          isTab = true;
          resetValue();
        }
      });
      return isTab;
    },

    // 清空表单关联项
    clearRefChild() {
      const { getElements: getElementsTab } = this.vanFormTable || {};
      const { getElements: getElementsForm } = this.vanFormGenerator || {};
      if (this.vanFormTable) {
        (this.clearRefsChild(getElementsTab) || this.clearRefsChild(getElementsForm));
      } else {
        this.clearRefsChild(getElementsForm);
      }
    },

    // 获取数据
    fetchRemoteChoice() {
      return new Promise((resolve, reject) => {
        this.getRemote()
          .then((d) => {
            // init option
            this.option = _.map(
              d, ({ key: field_value, value: field_label }) => ({
                field_value,
                field_label,
                has_fill: false,
                fill_value: ''
              })
            );
            this.filterOption = this.option;
            resolve();
          })
          .catch((err) => {
            reject(err);
          });
      });
    },

    // remote 选项
    getRemoteChoice() {
      return new Promise((resolve, reject) => {
        this.getRemote()
          .then((d) => {
            this.clearRefChild();
            // init option
            this.option = _.map(
              d, ({ key: field_value, value: field_label }) => ({
                field_value,
                field_label,
                has_fill: false,
                fill_value: ''
              })
            );
            this.filterOption = this.option;
            resolve();
          })
          .catch((err) => {
            reject(err);
          });
      });
    }
  }
};
